const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpack = require('clean-webpack-plugin');
const PurifyCssWebpack = require('purifycss-webpack');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

const glob = require('glob');
module.exports = {
    entry: {
        print: './src/print.js',
        main: './src/index.js'
    },
    output: {
        filename: '[name].bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    plugins: [
        new CleanWebpack(['dist']), //新增加
        new HtmlWebpackPlugin({
            title: "欢迎来到webpack4"
        }),
        // new PurifyCssWebpack({ //消除冗余代码
        //     // 首先保证找路径不是异步的,所以这里用同步的方法
        //     // path.join()也是path里面的方法,主要用来合并路径的
        //     // 'src/*.html' 表示扫描每个html的css
        //     paths: glob.sync(path.join(__dirname, 'src/*.html'))
        // }),
        new MiniCssExtractPlugin({
            filename: "[name].css",
            chunkFilename: "[id].css"
        })

    ],
    module: {
        rules: [{
                test: /\.css$/,
                use: [
                    MiniCssExtractPlugin.loader, 'css-loader',

                ]
            }, {
                test: /\.less$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    "css-loader",
                    "less-loader",
                ]
            },

            {
                test: /\.(png|svg|jpg|gif)$/, //正则匹配所有的图片 用feil-loader 解析
                use: [{
                        loader: 'file-loader',
                        options: { // 这里的options选项参数可以定义多大的图片转换为base64
                            outputPath: 'img' //定义输出的图片文件夹
                        }
                    }

                ]
            }
        ]
    },
    devServer: {
        //设置基本目录结构
        contentBase: path.resolve(__dirname, 'dist'),
        //服务器的IP地址，可以使用IP也可以使用localhost
        host: 'localhost',
        //服务端压缩是否开启
        compress: true,
        //配置服务端口号
        port: 8090
    }
};